from MLBC.database import Data_generator, Data_loader
from MLBC.linear_model import linear_regrassion, print_Para
from sklearn import linear_model

def generate_data():
    dg = Data_generator(mu = 0, gamma=0.03)
    param = dg.random_initialize()
    dg.generate('train.csv')
    dg.generate('test.csv')
    with open('param.txt', 'w') as f:
        f.write(str(param))

def train():
    X, Y = Data_loader().load_dataset_from_file('train.csv')
    print(X.shape)
    print(Y.shape)
    model = linear_regrassion(X, Y, learningRate=0.001, epoch=200000, show_plot=False)
    parameter = model.fit(show_loss=True, batch_size=10,check_point=5000)
    print_Para(parameter)
    print("#===================现在开始测试==================#")
    X_test, Y_test = Data_loader().load_dataset_from_file('test.csv')
    Y_predict = model.predict(X_test)
    print('在测试集中Y是：   ',[round(x ,6) for x in Y_test][:5])
    print('模型预测答案Y是： ',list(Y_predict)[:5])
    reg = linear_model.LinearRegression(fit_intercept=True)
    reg.fit(X,Y)
    print('sklearn 测试之后是：')
    print('X的系数为： ',reg.coef_,' 偏置项为： ', reg.intercept_)

if __name__ == '__main__':
    # generate_data()
    train()